#include "Image.h"
#include <highgui.h>

namespace Magick
{
	Image::Image() : _image(nullptr)
	{
		
	}

	Image::Image(const std::string &fileName) : _image(nullptr)
	{
		read(fileName);
	}

	Image::~Image()
	{
		if (_image != nullptr)
		{
			cvReleaseImage(&_image);
		}
	}

	void Image::read(const std::string &fileName)
	{
		if (_image != nullptr)
		{
			cvReleaseImage(&_image);
			_image = nullptr;
		}
		_image = cvLoadImage(fileName.c_str(), CV_LOAD_IMAGE_UNCHANGED);

		assert(_image != nullptr);
	}

	void Image::write(Blob* pBlob, const std::string& channelSeq)
	{
		if (_image == nullptr){
			throw Error("image not loaded");
		}
		if (pBlob == nullptr){
			throw Error("Blob is null");
		}
		
		pBlob->setData(_image, channelSeq);
	}

}